package gui;

import information.BorrowedInformation;
import information.OutRentedInformation;
import information.VinylInformation;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import quary.QuaryBridge;
import table.LoansView;
import table.MemberTable;
import table.PersonTable;
import table.RentedView;
import table.StorageTable;
import table.VinylTable;
import table.VinylView;

import controller.InformationListener;
import controller.SelectedTableController;
import controller.SelectedViewController;




public class MainFrame {

	private VinylView vinylView;
	private RentedView borrowedView;
	private LoansView rentedOutView;
	
	private VinylInformation vinylInfo;
	private BorrowedInformation borrowedInfo;
	private OutRentedInformation outRentedInfo;
	
	private VinylTable vinylTable;
	private MemberTable memberTable;
	private PersonTable personTable;
	private StorageTable storageTable;
	
	private JScrollPane contentPane;
	

	public MainFrame() {
		JFrame frame = new JFrame("VinylKlubb");
		frame.setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(850, 600);
					

		
		JPanel infoPane = new JPanel();
		frame.add(infoPane, BorderLayout.CENTER);
		
		contentPane = new JScrollPane();
		
		JPanel viewPane = new JPanel();
		frame.add(viewPane, BorderLayout.NORTH);
		viewPane.add(contentPane);
	
		QuaryBridge quaryBridge = new QuaryBridge();
		setupViews(quaryBridge);
		setupTables(quaryBridge);
		setupInfos(quaryBridge);
		
		ButtonGroup buttonGroup = new ButtonGroup();
		
		SelectedViewController viewController = new SelectedViewController(
				vinylView, borrowedView, rentedOutView,
				vinylInfo, borrowedInfo, outRentedInfo,  
				buttonGroup, infoPane); 
		
		SelectedTableController tableController = new SelectedTableController(
				vinylTable, memberTable, personTable, storageTable,
				null,
				buttonGroup, infoPane);
	
		
		InformationListener listener = new InformationListener(viewController);
		setupListeners(listener);
		
		JPanel controllerPanel = new JPanel(new FlowLayout());
		controllerPanel.add(viewController);
		controllerPanel.add(tableController);
		
		frame.add(controllerPanel, BorderLayout.SOUTH);
		
//		frame.pack();
		frame.setVisible(true);
		
	}
	
	public void setupViews(QuaryBridge quaryBridge) {
		this.vinylView = new VinylView(contentPane, quaryBridge);
		this.borrowedView = new RentedView(contentPane, quaryBridge);
		this.rentedOutView = new LoansView(contentPane, quaryBridge);
	}
	
	public void setupTables(QuaryBridge quaryBridge) {
		this.vinylTable = new VinylTable(contentPane, quaryBridge);
		this.memberTable = new MemberTable(contentPane, quaryBridge);
		this.personTable = new PersonTable(contentPane, quaryBridge);
		this.storageTable = new StorageTable(contentPane, quaryBridge);
	}
	
	public void setupListeners(InformationListener listener) {
		this.vinylView.addListSelectionListener(listener);
		this.borrowedView.addListSelectionListener(listener);
		this.rentedOutView.addListSelectionListener(listener);
	}
	
	public void setupInfos(QuaryBridge quaryBridge) {
		this.vinylInfo = new VinylInformation(quaryBridge);
		this.borrowedInfo = new BorrowedInformation(quaryBridge);
		this.outRentedInfo = new OutRentedInformation(quaryBridge);
	}
	

	
	public static void main(String[] args) {
		new MainFrame();

	}

}
